package antsclub.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import antsclub.common.Encrypt;
import antsclub.common.util.ConstUtil;
import antsclub.common.util.DateUtil;
import antsclub.common.util.StringUtil;
import antsclub.model.User;

import com.alibaba.fastjson.JSON;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;

import javax.servlet.http.HttpSession;

public class UserController extends Controller{
	public void index(){
		
	}
	public void save(){
		try{
			User user=getModel(User.class);
			String uname=getPara("user.uname");
			String pwd=getPara("user.pwd");
			if(uname==null||"".equals(uname)){
				setAttr("msg", "用户名不能为空");
				render("/user/register.jsp");
			}else if(pwd==null||"".equals(pwd)){
				setAttr("msg", "密码不能为空");
				render("/user/register.jsp");
			}else{
				user.set("id", UUID.randomUUID().toString());
				pwd= Encrypt.md5(pwd);
				user.set("pwd", pwd);
				
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
				String currentTime=df.format(new Date());// new Date()为获取当前系统时间
				user.set("createTime", currentTime);
				boolean b=user.save();
				if(b){
					List<User> l=User.dao.find("select id,nickname from user where uname='"
												+uname+"' and pwd='"+pwd+"'");
	
					if(l.size()>0){
						HttpSession session=getSession();
						String nickname=l.get(0).getStr("nickname");
						if(nickname!=null&&!("").equals(nickname)){
							uname=nickname;
						}
						session.setAttribute("uname", uname);
						session.setAttribute("uid", l.get(0).get("id"));
						setAttr("msg", "注册成功,欢迎: "+uname);
						render("/index.jsp");
					}else{
						setAttr("msg", "注册成功，请登录");
						render("/user/login.jsp");
					}
				}else{
					setAttr("msg", "注册失败");
					render("/user/register.jsp");
				}
			}
		}catch(Exception e){
			setAttr("msg", e);
			render("/user/register.jsp");
		}
		
		
	}	
	public void login(){
		String uname=getPara("user.uname");
		String pwd=getPara("user.pwd");
		if(uname==null||"".equals(uname)){
			setAttr("msg", "用户名不能为空");
			render("/user/login.jsp");
		}else if(pwd==null||"".equals(pwd)){
			setAttr("msg", "密码不能为空");
			render("/user/login.jsp");
		}else{
			pwd=Encrypt.md5(getPara("user.pwd"));
			List<User> l=User.dao.find("select id,nickname from user where uname='"
										+uname+"' and pwd='"+pwd+"'");

			if(l.size()>0){
				HttpSession session=getSession();
				String nickname=l.get(0).getStr("nickname");
				if(nickname!=null&&!("").equals(nickname)){
					uname=nickname;
				}
				session.setAttribute("uname", uname);
				session.setAttribute("uid", l.get(0).get("id"));
				setAttr("msg", "登陆成功,欢迎: "+uname);
				render("/index.jsp");
			}else{
				setAttr("msg", "用户名或者密码错误");
				render("/user/login.jsp");
			}
		}
	}
	public void logout(){
		getSession().removeAttribute("uid");
		getSession().removeAttribute("uname");
		redirect("/index.jsp");
	}
	
	//查看用户信息
	public void view() {
		String userId = getPara(0);
		String sql = "select * from user u where u.id = '" + userId + "'";
		List l = Db.find(sql);
		renderJson(l);
	}
	
	//修改基本信息
		public void update() {
			try {
				String uname = getPara("user.uname");
				String nickname = getPara("user.nickname");
				String email = getPara("user.email");
				if(StringUtil.isNullString(uname)) {
					setAttr("msg", ConstUtil.MESSAGE_UNAME_REQUIRED);
				} else {
					String sql = "select * from user where uname = ?";
					List<User> userList = User.dao.find(sql,uname);
					if(userList.size() > 0) {
						User user = userList.get(0);
						//获取当前时间，并格式化为：2013-04-25 22:46:12
						String currentTime = DateUtil.formatDate(new Date(),ConstUtil.YMD_DASH_WITH_FULL_TIME);
						user.set("modifyTime", currentTime);
						user.set("nickname", nickname);
						user.set("email", email);
						boolean b = user.update();
						
						if(b) {
							setAttr("msg", ConstUtil.MESSAGE_UPDATE_SUCCESS);
						} else {
							setAttr("msg", ConstUtil.MESSAGE_UPDATE_FAILD);
						}
					}
				}
				render(ConstUtil.URL_BASIC_PAGE);
			} catch(Exception e) {
				setAttr("msg", e);
				render(ConstUtil.URL_BASIC_PAGE);
			}
		}
		
		//修改密码
		public void updatePwd() {
			try {
				String uname = getPara("user.uname");
				String pwd = getPara("user.pwd");
				String newPwd = getPara("user.newPwd");
				String newPwd2 = getPara("user.newPwd2");
				if(StringUtil.isNullString(pwd)) {
					setAttr("msg", ConstUtil.MESSAGE_OLD_PASSWORD_REQUIRED);
				} else if(StringUtil.isNullString(newPwd)) {
					setAttr("msg", ConstUtil.MESSAGE_NEW_PASSWORD_REQUIRED);
				} else if(StringUtil.isNullString(newPwd2)) {
					setAttr("msg", ConstUtil.MESSAGE_NEW_PASSWORD_2_REQUIRED);
				} else {
					//验证用户是否存在
					pwd = Encrypt.md5(pwd);//加密旧密码
					String sql = "select * from user where uname = ?" + " and pwd = ?";
					List<User> userList = User.dao.find(sql,uname,pwd);
					if(userList.size() > 0) {
						if(newPwd.equals(newPwd2)) {
							User user = userList.get(0);
							//获取当前时间，并格式化为：2013-04-25 22:46:12
							String currentTime = DateUtil.formatDate(new Date(),ConstUtil.YMD_DASH_WITH_FULL_TIME);
							user.set("modifyTime", currentTime);
							newPwd = Encrypt.md5(newPwd);//加密新密码
							user.set("pwd", newPwd);
							boolean b = user.update();
							if(b) {
								setAttr("msg", ConstUtil.MESSAGE_UPDATE_SUCCESS);
							} else {
								setAttr("msg", ConstUtil.MESSAGE_UPDATE_FAILD);
							}
						} else {
							setAttr("msg", ConstUtil.MESSAGE_PASSWORD_NOT_SAME);
						}
					} else {
						setAttr("msg", ConstUtil.MESSAGE_UNAME_OR_PASSWORD_WRONG);
					}
				}
				render(ConstUtil.URL_PASSWORD_PAGE);
			} catch(Exception e) {
				setAttr("msg", e);
				render(ConstUtil.URL_PASSWORD_PAGE);
			}
		}
		
		//修改密码
		public void adminRevertPwd() {
			try {
				String uname = getPara("user.uname");
				String pwd = getPara("user.pwd");
				//验证用户是否存在
				String sql = "select * from user where uname = ?";
				List<User> userList = User.dao.find(sql,uname);
				if(userList.size() > 0) {
					User user = userList.get(0);
					//获取当前时间，并格式化为：2013-04-25 22:46:12
					String currentTime = DateUtil.formatDate(new Date(),ConstUtil.YMD_DASH_WITH_FULL_TIME);
					user.set("modifyTime", currentTime);
					String newPwd = Encrypt.md5(pwd);//重置密码
					user.set("pwd", newPwd);
					boolean b = user.update();
					if(b) {
						setAttr("msg", ConstUtil.MESSAGE_UPDATE_SUCCESS);
					} else {
						setAttr("msg", ConstUtil.MESSAGE_UPDATE_FAILD);
					}
				} else {
					setAttr("msg", ConstUtil.MESSAGE_UNAME_OR_PASSWORD_WRONG);
				}
				render(ConstUtil.URL_PASSWORD_PAGE);
			} catch(Exception e) {
				setAttr("msg", e);
				render(ConstUtil.URL_PASSWORD_PAGE);
			}
		}
}
